<?php
namespace app\data\controller\activity;
use app\data\model\ActActivity;
use app\data\model\ActOrder;
use app\data\model\ActSettingTheme;
use app\data\model\ActTicketDetails;
use app\data\model\ActTicketGroup;
use app\data\model\ActUserModel;
use app\index\controller\BaseController;

class activity extends BaseController
{
    /**
     * 列表
     * @menu true
     * @auth true
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
   
        $this->title = '活动列表';
        $query = request()->get();
        $where = [];
        if(!empty($query['user_name'])){
            $uids = ActUserModel::where('name|email','like',"%{$query['user_name']}%")->column('id');
            if(count($uids)){
                $where[] = ['create_user_id','in',$uids];
            }
        }
        if(isset($query['status']) && $query['status'] !=''){
            $where[] = ['status','=',$query['status']];
        }
         ActActivity::mQuery()
            ->with(['themeModel','userModel','guestModel'])
            ->order('id desc')
            ->where($where)
            ->like('name,organizer_name')
            ->append(['type_name','status_name','state'])
            ->page();
    }

   

    protected function  _index_page_filter(&$data) {
       foreach ($data as $key => &$value) {
            $theme_ids = array_column($value['themeModel'],'theme_id');
            $value['themes'] = ActSettingTheme::where('id','in',$theme_ids)->column('name');
       }
    }

        /**
     * 修改用户状态
     * @auth true
     */
    public function state()
    {
        ActActivity::mSave($this->_vali([
            'status.in:1,2'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * @name:普通票
     * @author:Sam
     * @date:2024-04-17
     * @param:[string] param [desc]
    */
    public function ticket(){
        $this->title = '票券列表';
        $query = request()->get();
        $where = [];
        if(isset($query['is_open']) && $query['is_open'] !=''){
            $where[] = ['is_open','=',$query['is_open']];
        }
        if(isset($query['name']) && $query['name'] !=''){
            $where[] = ['name','like',"%{$query['name']}%"];
        }
        if(isset($query['activity_id']) && $query['activity_id'] !=''){
            $where[] = ['activity_id','=',$query['activity_id']];
        }
        $this->query     = $query;
        $this->activitys = ActActivity::field('id,name')->select();
        ActTicketDetails::mQuery()->with(['activityModel','ticketModel'])->where($where)->append(['sale_status'])->order('id desc')->page();
    }

    protected function  _ticket_page_filter(&$data) {
        
        foreach ($data as $key => &$value) {
             
        }
     }

     /**
      * @name:票券分组
      * @author:Sam
      * @date:2024-04-17
      * @param:[string] param [desc]
     */
     public function group(){
        $this->title = '票券分组列表';
        $query = request()->get();
        $where = [];
        if(isset($query['is_open']) && $query['is_open'] !=''){
            $where[] = ['group_is_open','=',$query['is_open']];
        }
        if(isset($query['name']) && $query['name'] !=''){
            $where[] = ['group_name','like',"%{$query['name']}%"];
        }
        if(isset($query['activity_id']) && $query['activity_id'] !=''){
            $where[] = ['activity_id','=',$query['activity_id']];
        }
        $this->query     = $query;
        $this->activitys = ActActivity::field('id,name')->select();
        ActTicketGroup::mQuery()->with(['activityModel','ticketModel'])->where($where)->append(['sale_status'])->order('id desc')->page();
     }


    /**
     * @name:订单管理
     * @author:Sam
     * @date:2024-04-17
     * @param:[string] param [desc]
    */
     public function order(){
        $this->title = '报名管理';
        $query = request()->get();
        $where = [];
        if(isset($query['status']) && $query['status'] !=''){
            $where[] = ['status','=',$query['status']];
        }
        if(isset($query['ticket_name']) && $query['ticket_name'] !=''){
            $where[] = ['ticket_name','like',"%{$query['ticket_name']}%"];
        }
        if(isset($query['activity_id']) && $query['activity_id'] !=''){
            $where[] = ['activity_id','=',$query['activity_id']];
        }
        if(isset($query['user_name']) && $query['user_name'] !=''){
            $where[] = ['name|email|phone','like',"%{$query['user_name']}%"];
        }
        $this->query     = $query;
        $this->activitys = ActActivity::field('id,name')->select();
        ActOrder::mQuery()->with(['activityModel','user'])->where($where)->append(['status_name','diy_data'])->order('id desc')->page();
     }


     protected function  _order_page_filter(&$data) {
      
        foreach ($data as $key => &$value) {
             
        }
     }

     /**
      * @name:导出
      * @author:Sam
      * @date:2024-04-17
      * @param:[string] param [desc]
     */
     public function export(){
        $query = request()->get();
        $where = [];
        if(isset($query['status']) && $query['status'] !=''){
            $where[] = ['status','=',$query['status']];
        }
        if(isset($query['ticket_name']) && $query['ticket_name'] !=''){
            $where[] = ['ticket_name','like',"%{$query['ticket_name']}%"];
        }
        if(isset($query['activity_id']) && $query['activity_id'] !='' && $query['activity_id'] > 0){
            $where[] = ['activity_id','=',$query['activity_id']];
        }
        if(isset($query['user_name']) && $query['user_name'] !=''){
            $where[] = ['name|email|phone','like',"%{$query['user_name']}%"];
        }
        $title = ['编号', '活动', '创建时间','价格','票券','状态','用户','邮箱','电话','报名姓名','报名邮箱','报名手机','报名表'];
        set_time_limit(0); // 设置超时时间
        $filename = '報名名單' . date('Ymdhis') . '.csv';
        header("Content-type:application/vnd.ms-csv");
        header('content-disposition:attachment; filename="' . $filename . '"');
        ob_end_flush();
        echo iconv('utf-8', 'gbk', implode(',', $title)) . PHP_EOL;
        $lastId = 0;
        // dump($wheres);die;
        while (true) {
            $list =  ActOrder::with(['activityModel','user'])->where($where)->limit(100)->order('id asc')->where('id','>',$lastId)->field('id,sn,create_time,activity_id,price,ticket_name,status,name,email,phone,buy_user_id,group_id,diys')->append(['status_name'])->select()->toArray();   
            if (!empty($list)) {
              
                foreach ($list as $order) {
                    $item = []; // 初始化数组
                    // 用户信息
                    $item[] = $order['sn']; 
                    $item[] = $order['activityModel']['name'] ?? '';
                    $item[] = $order['create_time']; 
                    $item[] = $order['price']; 
                    $item[] = $order['ticket_name']; 
                    $item[] = $order['status_name']; 
                    $item[] = $order['user']['name']; 
                    $item[] = $order['user']['email']; 
                    $item[] = $order['user']['phone']; 
                    $item[] = $order['name']; 
                    $item[] = $order['email']; 
                    $item[] = $order['phone']; 
                    $item[] = $order['diys']; 
                    // dump($item);die;
                    // 写入内容
                    echo iconv('utf-8', 'gbk//IGNORE', implode(',', $item)) . PHP_EOL;
                    $lastId = $order['id']; // 最大id
                }

            } else {
                break;
            }
        }
            exit;
     }
}